package com.blb.controller;

import com.blb.entity.Company;
import com.blb.entity.User;
import com.blb.service.ICompanyService;
import com.blb.service.IUserService;
import com.blb.utils.ShiroUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("admininfo")
public class AdminInfoController {


    @Resource
    private IUserService userService;


    @Resource
    private ICompanyService companyService;



    @RequestMapping("")
    public String getHtml( Model model){

        User user = ShiroUtils.getUser();

        Map<String, Object> userAndCompany = userService.getUserAndCompanyByUser(user.getId());



            if (!userAndCompany.containsKey("company_brand")){
                userAndCompany.put("company_brand",null);
            }
            if (!userAndCompany.containsKey("company_type")){
                userAndCompany.put("company_type",null);
            }
            if (!userAndCompany.containsKey("company_scale")){
                userAndCompany.put("company_scale",null);
            }
            if (!userAndCompany.containsKey("fixed_tel")){
                userAndCompany.put("fixed_tel",null);
            }
            if (!userAndCompany.containsKey("legal_tel")){
                userAndCompany.put("legal_tel",null);
            }
            if (!userAndCompany.containsKey("company_qq")){
                userAndCompany.put("company_qq",null);
            }
            if (!userAndCompany.containsKey("company_wechat")){
                userAndCompany.put("company_wechat",null);
            }
            if (!userAndCompany.containsKey("longitude_latitude")){
                userAndCompany.put("longitude_latitude",null);
            }
            if (!userAndCompany.containsKey("company_introduction")){
                userAndCompany.put("company_introduction",null);
            }
            if (!userAndCompany.containsKey("company_address")){
                userAndCompany.put("company_address",null);
            }
        if (!userAndCompany.containsKey("company_image")){
            userAndCompany.put("company_image",null);
        }





       model.addAttribute("userAndCompany",userAndCompany);

        return "system/shopping_company";
    }

    @PostMapping("update")
    public String updateInfo(User user, Company company,HttpSession session){

        //获得登录者的id
        User user1 = ShiroUtils.getUser();
        String userId = user1.getId();
        String companyId = user1.getCompanyId();

        System.out.println(user);
        System.out.println(company);


        user.setId(userId);
        company.setId(companyId);
        userService.updateById(user);
        companyService.updateById(company);

        return "redirect:/admininfo";
    }

}
